package com.project.myPackage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.myPackage.common.R;
import com.project.myPackage.dto.UpdatePwdDto;
import com.project.myPackage.entity.Users;
import com.project.myPackage.service.UsersService;
import io.swagger.annotations.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@Api(tags = "用户管理")
@RestController
@Slf4j
@RequestMapping("/")
public class UsersController {
    @Autowired
    UsersService usersService;

    /**
     * 注册用户
     * @param users
     * @return
     */
    @ApiOperation(value = "注册用户")
    @PostMapping("/regUser")
    public R<String> regUser(@RequestBody Users users){
        if(users.getUsername()==null) return R.error("用户名不能为空");
        if(users.getPassword()==null) return R.error("密码不能为空");
        if(users.getNickname()==null) return R.error("姓名不能为空");
        if(users.getRole()==null) return  R.error("角色不能为空");
        if(users.getEmail()==null) return  R.error("邮箱不能为空");
        if(users.getPhone()==null) return  R.error("手机号不能为空");

        log.info("打印"+users);
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUsername, users.getUsername());
        int count = usersService.count(queryWrapper);
        if(count>0){
            return R.error("用户名被占用!");
        }else{
            users.setRole(1);//默认注册用户
            BigDecimal money = new BigDecimal(0);
            users.setMoney(money);//默认金额为0
            usersService.save(users);
            return R.success("注册成功");
        }
    }

    @ApiOperation(value = "用户登录")
   @PostMapping("/login")
    public R<Map<String, Object>> login(@RequestBody Users users){
        if(users.getUsername()==null) return R.error("用户名不能为空");
        if(users.getPassword()==null) return R.error("密码不能为空");
        if(users.getRole()==null) return R.error("角色不能为空");
       LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
       //比较用户名和密码是否一致
       queryWrapper.eq(Users::getPassword,users.getPassword());
       queryWrapper.eq(Users::getUsername,users.getUsername());
       queryWrapper.eq(Users::getRole,users.getRole());

       //是否查询成功
       Users one = usersService.getOne(queryWrapper);
       if(one!=null){
           return R.success("登录成功", one.getUser_id());
       //如何自定义一个对象

       }else{
           return R.error("用户名或密码错误");
       }
   }

  // 个人中心 获取用户基本信息 根据用户id 查询用户基本信息
    @ApiOperation(value = "获取用户基本信息")
   @GetMapping("/userInfo")
   public R<?> getUserInfo(Integer user_id){
       Users users = usersService.getById(user_id);
       if (users != null) {
           return R.success(users,"获取用户基本信息成功！");
       } else {
           return R.error("获取用户信息失败！");
       }
   }

    //更新用户基本信息
    @ApiOperation(value = "更新用户基本信息")
    @PostMapping("/updateUserInfo")
    public R<String> updateUserInfo(@RequestBody Users users){
        boolean b = usersService.updateById(users);
        if(b){
            return R.success("更新用户信息成功！");
        }else{
            return R.error("更新用户信息失败！");
        }

    }

    //管理员获取所有用户信息
    @ApiOperation(value = "管理员获取所有用户信息")
    @GetMapping("/getAllUserInfo")
    public R<List<Users>> getAllUserInfo(){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        //role不等于2
        queryWrapper.ne(Users::getRole,2);
        List<Users> list = usersService.list(queryWrapper);
        return R.success(list,"获取所有用户信息列表成功！");
    }

    //重置密码 oldPwd newPwd user_id
    @ApiOperation(value = "重置密码")
    @PostMapping("/updatePwd")
    public R<String> updatePwd(@Valid @RequestBody UpdatePwdDto updatePwdDto ){
        if(updatePwdDto.getUser_id()==null)return R.error("用户ID不能为空！");
        if(updatePwdDto.getOldPwd().equals(updatePwdDto.getNewPwd())) return R.error("新密码不能与旧密码相同！");

        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUser_id, updatePwdDto.getUser_id());
        queryWrapper.eq(Users::getPassword, updatePwdDto.getOldPwd());

        Users one = usersService.getOne(queryWrapper);
        if(one!=null){
            one.setPassword(updatePwdDto.getNewPwd());
            boolean b = usersService.updateById(one);
            if (!b){
                return R.error("重置密码失败！");
            }
            return R.success("重置密码成功！");
        }else{
            return R.error("旧密码错误！");
        }
    }
}


